package adc.app.spec;

/**
 * Exception thrown while invalid an invalid username/password pair is provided
 * in a login attempt.
 * 
 * @author Alex
 * 
 */
public class InvalidCredentialException extends BsException {

	private static final long serialVersionUID = 1L;

	private String username;
	private Integer passwordLength;

	/**
	 * Create an instance of this class by providing the username, the length of the
	 * password and the exception causing the login fail.
	 * @param username the username
	 * @param passwordLength the password's length
	 * @param cause the exception cause
	 */
	public InvalidCredentialException(String username, int passwordLength,
			Throwable cause) {
		super(getMessage(username, passwordLength), cause);
		this.username = username;
		this.passwordLength = passwordLength;
	}

	/**
	 * Create an instance of this class by providing the username and the length of the
	 * password.
	 * @param username the username
	 * @param passwordLength the password's length
	 */
	public InvalidCredentialException(String username, int passwordLength) {
		this(username, passwordLength, null);
	}

	/**
	 * Getter for the username.
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Getter for the password's length.
	 * @return the password's length
	 */
	public Integer getPasswordLength() {
		return passwordLength;
	}

	/**
	 * Create the error message from the username and the password's length.
	 * @param username the username
	 * @param passwordLength the password's length
	 * @return the error message
	 */
	public static String getMessage(String username, Integer passwordLength) {
		StringBuffer stringBuffer = new StringBuffer(
				"Invalid credentials username: ");
		stringBuffer.append(username);
		if (passwordLength != null && passwordLength > 0) {
			stringBuffer.append(passwordLength);
			for (int i = 0; i < passwordLength; i++) {
				stringBuffer.append("*");
			}
		}
		return stringBuffer.toString();
	}

}
